package com.zhidisoft.taxpayer.springmvccontroller;

import com.zhidisoft.manage.dao.IndustryDao;
import com.zhidisoft.manage.dao.TaxOrganDao;
import com.zhidisoft.manage.dao.TaxPayerDao;
import com.zhidisoft.manage.dao.TaxSourceDao;
import com.zhidisoft.manage.entity.TaxPayer;
import com.zhidisoft.manage.entity.TaxSource;
import com.zhidisoft.system.dao.TaxerDao;
import com.zhidisoft.system.entity.User;
import com.zhidisoft.util.FormatDate;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class TaxPayerSpringMvc {
    /**
     * 返回纳税人详情信息
     *
     * @return
     */
    @RequestMapping("/getTaxPayerInfo.do")
    public String getTaxPayerInfo(String id, Model model) {
        TaxPayer payer = TaxPayerDao.getPayer(id);
        model.addAttribute("payer", payer);
        model.addAttribute("user", TaxerDao.getTaxerById(payer.getUserId()));
        model.addAttribute("organ", TaxOrganDao.getOrgan(payer.getTaxOrganId()));
        model.addAttribute("industry", IndustryDao.getIndustry(payer.getIndustryId()));
        return "payerInfo";
    }

    /**
     * 删除纳税人
     */
    @RequestMapping("/deleteTaxPayer.do")
    @ResponseBody
    public ModelMap deleteTaxPayer(String id) {
        ModelMap modelMap = new ModelMap();
        boolean isExistTask = TaxSourceDao.getSourceByPlayer(id);
        if (isExistTask) {
            modelMap.put("success", false);
            modelMap.put("msg", "还有任务未处理完毕");
        } else {
            boolean b = TaxPayerDao.deletePayer(id);
            if (b) {
                modelMap.put("success", true);
                modelMap.put("msg", "删除成功");
            } else {
                modelMap.put("success", false);
                modelMap.put("msg", "删除失败");
            }
        }
        return modelMap;
    }

    /**
     * 拿要更新纳税人信息
     */
    @RequestMapping("/toEditTaxPayer.do")
    public String toEditTaxPayer(String id, Model model) {
        TaxPayer payer = TaxPayerDao.getPayer(id);
        model.addAttribute("payer", payer);
        //获取纳税人对象信息
        model.addAttribute("industry", IndustryDao.getIndustry(payer.getIndustryId()));
        model.addAttribute("organ", TaxOrganDao.getOrgan(payer.getTaxOrganId()));
        model.addAttribute("user", TaxerDao.getTaxerById(payer.getUserId()));
        model.addAttribute("organs", TaxOrganDao.getOrgans());
        model.addAttribute("industrys", IndustryDao.getIndustrys());
        return "editTaxPayer";
    }

    /**
     * 更新纳税人
     */
    @RequestMapping("/editTaxPayer.do")
    @ResponseBody
    public ModelMap editTaxPayer(HttpSession session, String id, String payerCode, String payerName, String bizAddress, String taxOrganId, String industryId, String bizScope, String invoiceType,
                                 String legalPerson, String legalIdCard, String finaceName, String finaceIdCard, String taxerName, String bizAddressPhone, HttpServletRequest req) {
        ModelMap modelMap = new ModelMap();
        TaxPayer payer = new TaxPayer();

        payer.setId(Integer.parseInt(id));

        payer.setPayerCode(payerCode);

        payer.setPayerName(payerName);

        payer.setBizAddress(bizAddress);

        if ("-1".equals(taxOrganId)) {
            payer.setTaxOrganId(null);
        } else {
            payer.setTaxOrganId(Integer.parseInt(taxOrganId));
        }
        if ("-1".equals(industryId)) {
            payer.setIndustryId(null);
        } else {
            payer.setIndustryId(Integer.parseInt(industryId));
        }

        payer.setBizScope(bizScope);

        payer.setInvoiceType(invoiceType);

        payer.setLegalPerson(legalPerson);

        payer.setLegalIdCard(legalIdCard);

        payer.setFinaceName(finaceName);

        payer.setFinaceIdCard(finaceIdCard);

        payer.setTaxerName(taxerName);

        payer.setBizAddressPhone(bizAddressPhone);
        Date date = new Date();
        payer.setRecordDate(FormatDate.format(date));
        User user = (User) session.getAttribute("user");
        payer.setUserId(user.getId());

        boolean b = TaxPayerDao.updatePayer(payer, Integer.parseInt(id));
        if (b) {
            modelMap.addAttribute("success", true);
            modelMap.addAttribute("msg", "保存成功");
        } else {
            modelMap.addAttribute("success", true);
            modelMap.addAttribute("msg", "保存失败");
        }
        return modelMap;
    }

    /**
     * 拿数据到新建任务中
     */
    @RequestMapping("/toAddTask.do")
    public String toAddTask(String id, String payerCode, Model model) {
        TaxPayer payer = null;
        if (StringUtils.isNotEmpty(payerCode)) {
            payer = TaxPayerDao.getPayerByCode(payerCode);
        }
        if (StringUtils.isNotEmpty(id)) {
            payer = TaxPayerDao.getPayer(id);
            TaxSource task = TaxSourceDao.getTask(Integer.parseInt(id));
            model.addAttribute("task", task);
        }
        if (payer != null) {
            model.addAttribute("payer", payer);
            if (payer.getTaxOrganId() != null)
                model.addAttribute("organ", TaxOrganDao.getOrgan(payer.getTaxOrganId()));
            if (payer.getIndustryId() != null)
                model.addAttribute("industry", IndustryDao.getIndustry(payer.getIndustryId()));
        }
        model.addAttribute("taxers", TaxerDao.getTaxers());//所有人
        model.addAttribute("organs", TaxOrganDao.getOrgans());//所有下达部门
        return "addTask";
    }

    /**
     * 新建任务上传
     */
    @RequestMapping("/addTask.do")
    @ResponseBody
    public ModelMap addTask(HttpServletRequest req, HttpSession session) {
        ModelMap modelMap = new ModelMap();
        // 获取参数，创建任务对象，未选的下拉框判断值是否为-1
        TaxSource task = new TaxSource();
        String payerId = req.getParameter("payerId");
        if (payerId == null || payerId.length() == 0) {
            task.setPayerId(null);
        } else {
            task.setPayerId(Integer.parseInt(payerId));
        }

        String taskName = req.getParameter("taskName");
        task.setTaskName(taskName);

        String sub = req.getParameter("subOrganId");
        if (sub != null) {
            int subOrganId = Integer.parseInt(sub);
            if ("-1".equals(subOrganId)) {
                task.setSubOrganId(null);
            } else {
                task.setSubOrganId(subOrganId);
            }
        }

        String approverId = req.getParameter("approverId");
        if ("-1".equals(approverId)) {
            task.setApproverId(null);
        } else if (approverId != null) {
            task.setApproverId(Integer.parseInt(approverId));
        }

        String executeId = req.getParameter("executeId");
        if ("-1".equals(executeId)) {
            task.setExecuteId(null);
        } else if (executeId != null) {
            task.setExecuteId(Integer.parseInt(executeId));
        }
        String executeTime = req.getParameter("executeTime");
        task.setExecuteTime(executeTime);
        String taskState = req.getParameter("taskState");
        task.setTaskState(taskState);

        User user = (User) session.getAttribute("user");
        if (user != null) {
            Integer recordUserId = user.getId();
            task.setRecordUserId(recordUserId);
        }
        Date date = new Date();
        task.setRecordTaskDate(FormatDate.format(date));
        // 获取插入的任务id，如果返回-1，则插入失败
        int id = TaxSourceDao.getId(task);
        if (id != -1) {
            modelMap.addAttribute("success", true);
            modelMap.addAttribute("id", id);
            modelMap.addAttribute("msg", "添加任务成功");
        } else {
            modelMap.addAttribute("success", false);
            modelMap.addAttribute("msg", "添加失败");
        }
        return modelMap;
    }

    /**
     * 去添加纳税人页面
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/toAddTaxPayer.do")
    public String toAddTaxPayer(Model model, HttpSession session) {
        model.addAttribute("organs", TaxOrganDao.getOrgans());
        model.addAttribute("industrys", IndustryDao.getIndustrys());
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", TaxerDao.getTaxerByUserId(user.getId()));
        model.addAttribute("curDate", FormatDate.format(new Date()));
        return "addTaxPayer";
    }

    /**
     * 添加纳税人
     *
     * @param session
     * @param legalIdCardImageURL
     * @param payerCode
     * @param payerName
     * @param bizAddress
     * @param taxOrganId
     * @param industryId
     * @param bizScope
     * @param invoiceType
     * @param legalPerson
     * @param legalIdCard
     * @param finaceName
     * @param finaceIdCard
     * @param taxerName
     * @param bizAddressPhone
     * @param finaceIdCardImageURL
     * @return
     */
    @RequestMapping("/addTaxPayer.do")
    @ResponseBody
    public ModelMap addTaxPayer(HttpSession session, String legalIdCardImageURL, String payerCode, String payerName, String bizAddress, String taxOrganId, String industryId, String bizScope, String invoiceType,
                                String legalPerson, String legalIdCard, String finaceName, String finaceIdCard, String taxerName, String bizAddressPhone, String finaceIdCardImageURL) {
        ModelMap modelMap = new ModelMap();
        TaxPayer payer = new TaxPayer();
        payer.setPayerCode(payerCode);
        payer.setPayerName(payerName);
        payer.setBizAddress(bizAddress);

        if ("-1".equals(taxOrganId)) {
            payer.setTaxOrganId(null);
        } else {
            payer.setTaxOrganId(Integer.parseInt(taxOrganId));
        }

        if ("-1".equals(industryId)) {
            payer.setIndustryId(null);
        } else {
            payer.setIndustryId(Integer.parseInt(industryId));
        }

        payer.setBizScope(bizScope);
        payer.setInvoiceType(invoiceType);
        payer.setLegalPerson(legalPerson);
        payer.setLegalIdCard(legalIdCard);
        payer.setLegalIdCardImageURL(legalIdCardImageURL);
        payer.setFinaceName(finaceName);
        payer.setFinaceIdCard(finaceIdCard);
        payer.setFinaceIdCardImageURL(finaceIdCardImageURL);
        payer.setTaxerName(taxerName);
        payer.setBizAddressPhone(bizAddressPhone);
        Date date = new Date();
        payer.setRecordDate(FormatDate.format(date));
        User user = (User) session.getAttribute("user");
        payer.setUserId(user.getId());
        //保存
        TaxPayerDao tpd = new TaxPayerDao();
        boolean b = tpd.addPayer(payer);
        if (b) {
            modelMap.addAttribute("success", true);
            modelMap.addAttribute("msg", "添加成功");
        } else {
            modelMap.addAttribute("success", false);
            modelMap.addAttribute("msg", "添加失败");
        }

        return modelMap;
    }

}
